﻿/* 功能:  商户管理
 * 创建人：Kencery  创建时间：2017-12-9
 */
var ml = window.ml || {};
ml.admin = ml.admin || {};

ml.admin.merchant = (function () {
    var defaults = {
        listUrl: "/Merchant/List",
        getReceivablesByMerchantIdUrl: "/Merchant/GetReceivablesByMerchantId",
        getBusinessActivitiesByMerchantIdUrl: "/Merchant/GetBusinessActivitiesByMerchantId",
        getProductCategoryByMerchantIdUrl: "/Merchant/GetProductCategoryByMerchantId",
        detailUrl: "/Merchant/GetDetailById",
        delUrl: "/Merchant/Delete"
    };
    var opt = {};

    var columns = [{
        field: 'state',
        radio: true
    }, {
        title: '行号',
        field: 'number',
        align: 'center',
        hide: true,
        formatter: plscommon.tableNumber
    }, {
        title: '商户ID',
        field: 'Id',
    }, {
        title: '用户名',
        field: 'NickName',
        align: 'center',
    }, {
        title: '平台分类名称',
        field: 'PlatFormName',
        align: 'center',
    }, {
        title: '商户名',
        field: 'Name',
        align: 'center',
    }, {
        title: '商户Logo',
        field: 'Logo',
        align: 'center',
        formatter: function (value, rows, index) {
            return '<div><img src="' + value + '" style="width:40px;height:40px;"></div>';
        }
    }, {
        title: '电话',
        field: 'Phone',
        align: 'center',
    }, {
        title: '营业开始时间',
        field: 'BusinessStartHours',
        align: 'center',
    }, {
        title: '结束时间',
        field: 'BusinessEndHours',
        align: 'center',
    }, {
        title: '配送费',
        field: 'DistributionFee',
        align: 'center',
    }, {
        title: '营业',
        field: 'JoinMark',
        align: 'center',
        formatter: function (value, rows, index) {
            var result = returnJoinMark(value);
            return '<div id="State_' + rows.Id + '">' + result + '</div>';
        }
    }, {
        title: '创建',
        field: 'CreateTime',
        align: 'center',
    }];

    var initTable = function () {
        plscommon.bootstraptable({
            id: "#merchantinfo",
            url: defaults.listUrl,
            queryParams: queryParams,
            uniqueId: "Id",
            columns: columns
        });
    }

    var queryParams = function (params) {
        return {
            offset: params.offset,             //后台计算显示数据信息
            pagesize: params.limit,            //每页显示多少行
            nickname_search: $("#nickname_search").val(),
            merchantname_search: $("#merchantname_search").val(),
            joinmark_search: $("#joinmark_search").val()
        };
    };

    var clickEvent = function () {
        $("#btnQueryList").on("click", function () { btnQueryList(); });                      //按条件查询结果
        $("#btnReset").on("click", function () { btnReset(); });                              //清空文本框信息

        $("#btnReceivables").on("click", function () { btnReceivables(); });                  //查看收款方式
        $("#btnBusinessActivities").on("click", function () { btnBusinessActivities(); });    //查看商户活动
        $("#btnProductCategory").on("click", function () { btnProductCategory(); });          //查看商户产品分类
        $("#btnDetail").on("click", function () { btnDetail(); });                            //订单详情

        $("#btnDeleteDialog").on("click", function () { btnDeleteDialog(); });                //删除弹出浮层
        $("#btnDeleteOK").on("click", function () { btnDeleteOK(); });                        //删除确定
    };

    var btnQueryList = function () {
        plscommon.refreshTable("merchantinfo");
    }

    var btnReset = function () {
        plscommon.resetFrom("formSearch");
    }

    var btnReceivables = function () {
        var data = plscommon.dialogWaring('merchantinfo', 'QueryReceivables', 'QueryReceivablesOperation');
        if (!data.Id) {
            return false;
        }
        $("#HeadTitle_Receivables").text("(" + data.Name + ")商户收款方式");

        //发送请求调用查询后台返回数据
        $("#btnReceivables").prop("disabled", true);
        plscommon.ajax({
            disableId: "btnReceivables",
            url: defaults.getReceivablesByMerchantIdUrl,
            type: "GET",
            data: { id: data.Id },
            success: function () {
                var data = this.data;
                if (data.length > 0) {
                    $("#QueryReceivablesTable").show();
                    $("#QueryReceivablesTableNo").hide();

                    //不为空写入查询出来的内容
                    $("#QueryReceivablesTable table tbody").html("");
                    var tbody_tr = "";
                    $.each(data, function (i, item) {
                        var defaultmarktext = item.DefaultMark == 0 ? "未默认" : "默认";
                        tbody_tr += "<tr><td>" + item.Method + "</td><td>" + item.OpeningBank + "</td><td>" + item.OpeningName + "</td>"
                            + "<td>" + item.OpeningAccount + "</td><td>" + item.ThirdAccount + "</td>"
                            + "<td>" + defaultmarktext + "</td><td>" + item.CreateTime + "</td></tr>";
                    });
                    $("#QueryReceivablesTable table tbody").append(tbody_tr);
                } else {
                    $("#QueryReceivablesTable").hide();
                    $("#QueryReceivablesTableNo").show();
                }
            }
        });
    }

    var btnBusinessActivities = function () {
        var data = plscommon.dialogWaring('merchantinfo', 'QueryBusinessActivities', 'QueryBusinessActivitiesOperation');
        if (!data.Id) {
            return false;
        }
        $("#HeadTitle_BusinessActivities").text("(" + data.Name + ")商户活动");

        //发送请求调用查询后台返回数据
        $("#btnBusinessActivities").prop("disabled", true);
        plscommon.ajax({
            disableId: "btnBusinessActivities",
            url: defaults.getBusinessActivitiesByMerchantIdUrl,
            type: "GET",
            data: { id: data.Id },
            success: function () {
                var data = this.data;
                if (data.length > 0) {
                    $("#QueryBusinessActivitiesTable").show();
                    $("#QueryBusinessActivitiesTableNo").hide();

                    //不为空写入查询出来的内容
                    $("#QueryBusinessActivitiesTable table tbody").html("");
                    var tbody_tr = "";
                    $.each(data, function (i, item) {
                        tbody_tr += "<tr><td>" + returnBusinessActivitiesType(item.Type) + "</td><td>" + item.Tag + "</td>"
                            + "<td>" + item.Content + "</td ><td>" + item.Sort + "</td><td>" + item.CreateTime + "</td></tr>";
                    });
                    $("#QueryBusinessActivitiesTable table tbody").append(tbody_tr);
                } else {
                    $("#QueryBusinessActivitiesTable").hide();
                    $("#QueryBusinessActivitiesTableNo").show();
                }
            }
        });
    }

    var btnProductCategory = function () {
        var data = plscommon.dialogWaring('merchantinfo', 'QueryProductCategory', 'ProductCategoryOperation');
        if (!data.Id) {
            return false;
        }
        $("#HeadTitle_ProductCategory").text("(" + data.Name + ")商户产品分类");

        //发送请求调用查询后台返回数据
        $("#btnProductCategory").prop("disabled", true);
        plscommon.ajax({
            disableId: "btnProductCategory",
            url: defaults.getProductCategoryByMerchantIdUrl,
            type: "GET",
            data: { id: data.Id },
            success: function () {
                var data = this.data;
                if (data.length > 0) {
                    $("#ProductCategoryTable").show();
                    $("#ProductCategoryTableNo").hide();

                    $("#ProductCategoryTable table tbody").html("");
                    var tbody_tr = "";
                    $.each(data, function (i, item) {
                        tbody_tr += "<tr><td>" + item.Name + "</td><td>" + item.Sort + "</td>"
                            + "<td>" + item.CreateTime + "</td>"
                            + "<td>" + plscommon.extend.disable(item.Enable, "enable" + item.Enable) + "</td ><tr>"
                    });
                    $("#ProductCategoryTable table tbody").append(tbody_tr);
                } else {
                    $("#ProductCategoryTable").hide();
                    $("#ProductCategoryTableNo").show();
                }
            }
        });
    }

    var btnDetail = function () {
        //必须选中一条才能弹出
        var data = plscommon.dialogWaring('merchantinfo', 'DetailModal', 'MerchantDetail');
        if (!data.Id) {
            return false;
        }

        plscommon.ajax({
            url: defaults.detailUrl,
            type: "GET",
            data: { id: data.Id },
            success: function () {
                var data = this.data;
                $("#detail_nickname").val(data.NickName);
                $("#detail_platformname").val(data.PlatFormName);
                $("#detail_name").val(data.Name);
                $("#detail_phone").val(data.Phone);
                $("#detail_notice").val(data.Notice);
                $("#detail_address").val(data.Address);
                $("#detail_businessstarthours").val(data.BusinessStartHours);
                $("#detail_businesssendhours").val(data.BusinessEndHours);
                $("#detail_distributionfee").val(data.DistributionFee);
                $("#detail_joinmark").val(returnJoinMark(data.JoinMark));
                $("#detail_createtime").val(data.CreateTime);
                $("#detail_logo").attr("src", data.Logo);
            }
        });
    }

    var btnDeleteDialog = function () {
        var data = plscommon.dialogWaring('merchantinfo', 'DelMerchantModal', 'DelMerchantOperation');
        if (!data.Id) {
            return false;
        }
        $("#delete_id").val(data.Id);
        $("#Delete_Head_Title").text(data.Name);
        $("#Delete_Head_Title_OK").text(data.Name);
    }

    var btnDeleteOK = function () {
        var delete_id = $("#delete_id").val();
        if (!delete_id) {
            plscommon.warningMessage("请您选择需要删除的数据", 4000);
            return false;
        }

        $("#btnDeleteOK").prop("disabled", true);
        plscommon.ajax({
            disableId: "btnDeleteOK",
            url: defaults.delUrl,
            type: "POST",
            data: { id: delete_id },
            success: function () {
                $('#DelMerchantModal').modal('hide');
                $("#merchantinfo").bootstrapTable('refresh');
            }
        });
    }

    var returnJoinMark = function (value) {
        var result = "";
        if (value == 0) {
            result = "未营业";
        }
        if (value == 1) {
            result = "营业中";
        }
        return result;
    }

    var returnBusinessActivitiesType = function (value) {
        var result = "";
        if (value == 1) {
            result = "系统活动";
        }
        if (value == 2) {
            result = "商户活动";
        }
        return result;
    }

    return {
        init: function (options) {
            $.extend(defaults, options || {});
            initTable();            //初始化Table表格
            clickEvent();           //触发事件
        }
    };
}());